import QtQuick 2.15
import QtCharts 2.15

ChartView {
    id: chartView
    title: "实时动态曲线"
    antialiasing: true
    
    property var dataPoints: []
    property int maxPoints: 30
    
    SplineSeries {
        id: splineSeries
        name: "Spline"
        axisX: axisX
        axisY: axisY
    }
    
    ScatterSeries {
        id: scatterSeries
        name: "Scatter"
        markerSize: 8
        axisX: axisX
        axisY: axisY
    }
    
    ValueAxis {
        id: axisX
        min: 0
        max: 100
    }
    
    ValueAxis {
        id: axisY
        min: 0
        max: 100
    }
    
    Timer {
        id: dataTimer
        interval: 1000
        running: true
        repeat: true
        onTriggered: {
            var newValue = Math.random() * 100
            dataPoints.push(newValue)
            
            if (dataPoints.length > maxPoints) {
                dataPoints.shift()
            }
            
            splineSeries.clear()
            scatterSeries.clear()
            
            var dx = 100 / 10
            var less = 10 - dataPoints.length
            
            for (var i = 0; i < dataPoints.length; i++) {
                var x = (less * dx + i * dx)
                splineSeries.append(x, dataPoints[i])
                scatterSeries.append(x, dataPoints[i])
            }
        }
    }
}

